#include "MyListView.h"

#include <qheader.h>
#include <qlabel.h>
#include <qpainter.h>
#include <qapplication.h>
#include <qcursor.h>

#include "MyListViewItem.h"
#include "MyWidget.h"

MyListView::MyListView(QWidget * parent)
	: QListView(parent)
	, hoverPhone(false)
	, pixmapPhoneNormal(QPixmap::fromMimeSource("images/phone_normal.png"))
	, pixmapPhoneSelected(QPixmap::fromMimeSource("images/phone_selected.png"))
	, pixmapPhoneHover(QPixmap::fromMimeSource("images/phone_hover.png"))
	, pixmapStatusAvailable(QPixmap::fromMimeSource("images/status_available.png"))
	, pixmapStatusUnavailable(QPixmap::fromMimeSource("images/status_unavailable.png"))
	, pixmapAvatar(QPixmap::fromMimeSource("images/avatar.png"))
	, m_bShowPicture(false)
	, m_bSortByName(false)
{
	setAllColumnsShowFocus(true);
	setTreeStepSize(0);
	setSorting(0, true);
	setResizeMode(QListView::LastColumn);
	setFrameShape(QFrame::NoFrame);
	addColumn("");
	header()->hide();

	setCursor(Qt::PointingHandCursor);

	myWidget = new MyWidget;
	myWidget->hide();

	QFont font("Arial", 9);
#ifdef Q_WS_WIN
	font.setPixelSize(120);
#endif
#ifdef Q_WS_X11
	font.setPixelSize(14);
#endif
	setFont(font);
}

MyListView::~MyListView()
{
//	qDebug("~MyListView");
	delete myWidget;
}

bool MyListView::eventFilter(QObject *obj, QEvent *event)
{
    if (event->type() == QEvent::MouseMove)
	{
		QMouseEvent * mouseEvent = (QMouseEvent *)event;
		MyListViewItem * item = (MyListViewItem *)itemAt(mouseEvent->pos());
		if (item)
		{
			if (isSelected(item) && item->m_bHavePhone)
			{
				QRect r = itemRect(item);

				int x = mouseEvent->x() - r.x();
				int y = mouseEvent->y() - r.y();

				QFontMetrics fm = fontMetrics();
    			QPixmap & pixmapPhone = hoverPhone ? pixmapPhoneHover : pixmapPhoneNormal;

				QRect phoneRect(
					r.width() - MyListViewItem::c_nMargin - (MyListViewItem::c_nSps + fm.height() + MyListViewItem::c_nSps) + ((MyListViewItem::c_nSps + fm.height() + MyListViewItem::c_nSps - pixmapPhone.width()) >> 1)
						- (m_bShowPicture ? (r.height() - 2 * MyListViewItem::c_nMargin) : 0),
					MyListViewItem::c_nMargin + ((fm.height() - pixmapPhone.height()) >> 1),
					pixmapPhone.width(),
					pixmapPhone.height());

				if (!hoverPhone)
				{
					if (phoneRect.contains(x, y))
					{
						hoverPhone = true;
						qDebug("hover");
						repaintItem(item);
					}
				}
				else
				{
					if (!phoneRect.contains(x, y))
					{
						hoverPhone = false;
						repaintItem(item);
					}
				}
			}
			else
			{
				setSelected(item, TRUE);
				myWidget->show();

				QPoint point = mapToGlobal(QPoint(header()->sectionPos(0), itemPos(item) - contentsY()));
				myWidget->move(
				    point.x() < (QApplication::desktop()->width() - myWidget->width()) / 2 ? point.x() + header()->width() : point.x() - myWidget->width(),
		    		QMIN(point.y(), QApplication::desktop()->height() - myWidget->height()));


				if (hoverPhone)
				{
					hoverPhone = false;

					QListViewItem * selItem = selectedItem();
					if (selItem)
						repaintItem(selItem);
				}
			}
		}
		else
		{
			setSelected(item, FALSE);
			myWidget->hide();
		}
    }
	else if (event->type() == QEvent::MouseButtonPress && ((QMouseEvent *)event)->button() == QMouseEvent::LeftButton)
	{
		QMouseEvent * mouseEvent = (QMouseEvent *)event;
		MyListViewItem * item = (MyListViewItem *)itemAt(mouseEvent->pos());
		if (item)
		{
			if (isSelected(item) && item->m_bHavePhone)
			{
				QRect r = itemRect(item);

				int x = mouseEvent->x() - r.x();
				int y = mouseEvent->y() - r.y();

				QFontMetrics fm = fontMetrics();
    			QPixmap & pixmapPhone = hoverPhone ? pixmapPhoneHover : pixmapPhoneNormal;

				QRect phoneRect(
					r.width() - MyListViewItem::c_nMargin - (MyListViewItem::c_nSps + fm.height() + MyListViewItem::c_nSps) + ((MyListViewItem::c_nSps + fm.height() + MyListViewItem::c_nSps - pixmapPhone.width()) >> 1)
						- (m_bShowPicture ? (r.height() - 2 * MyListViewItem::c_nMargin) : 0),
					MyListViewItem::c_nMargin + ((fm.height() - pixmapPhone.height()) >> 1),
					pixmapPhone.width(),
					pixmapPhone.height());

				if (phoneRect.contains(x, y))
				{
					qDebug("click");
					return TRUE;
				}
			}
		}
	}

    return QListView::eventFilter(obj, event); // don't eat event
}

void MyListView::leaveEvent(QEvent *)
{
	setSelected(selectedItem(), FALSE);
	myWidget->hide();
}
